﻿#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type represents a user control realizing the IView contract.
	/// </summary>
	public class ViewUserControl : UserControl,
		IView
	{
		// data
		private readonly ViewDispatcher mDispatcher;


		#region ++ IView Interface ++

		/// <summary>
		/// Gets or sets the view model associated with the view.
		/// </summary>
		/// 
		/// <exception cref="InvalidOperationException">
		/// The calling thread does not have access to this System.Windows.Threading.DispatcherObject.</exception>
		public Object ViewModel
		{
			get
			{
				base.VerifyAccess();
				return this.DataContext;
			}
			set
			{
				base.VerifyAccess();
				this.DataContext = value;
			}
		}

		/// <summary>
		/// Gets a reference to the control that implements the view.
		/// </summary>
		/// 
		/// <exception cref="InvalidOperationException">
		/// The calling thread does not have access to this System.Windows.Threading.DispatcherObject.</exception>
		public FrameworkElement Control
		{
			get
			{
				base.VerifyAccess();
				return this;
			}
		}


		/// <summary>
		/// Executes the specified action synchronously on the thread that the view was created on.
		/// </summary>
		/// 
		/// <param name="action">
		/// The action to execute, which takes no arguments and does not return a value.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public void Invoke(Action action)
		{
			ViewHelper.Invoke(this, action);
		}

		/// <summary>
		/// Executes the specified action asynchronously on the thread that the view was created on.
		/// </summary>
		/// 
		/// <param name="action">
		/// The action to execute, which takes no arguments and does not return a value.</param>
		/// 
		/// <returns>
		/// A task that represents the asynchronously running action. This task can be used to wait for completion, 
		/// to register a continuation and to access the state of the action.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public Task InvokeAsync(Action action)
		{
			return ViewHelper.InvokeAsync(this, action);
		}


		/// <summary>
		/// Sends the specified view action synchronously to the thread that the view was created on, and 
		/// raises the view action attached event on the view.
		/// </summary>
		/// 
		/// <param name="viewAction">
		/// The view action to send to the view.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public void Send(ViewAction viewAction)
		{
			ViewHelper.Send(this, viewAction);
		}

		/// <summary>
		/// Sends the specified view action asynchronously to the thread that the view was created on, and 
		/// raises the view action attached event on the view.
		/// </summary>
		/// 
		/// <param name="viewAction">
		/// The view action to send to the view.</param>
		/// 
		/// <returns>
		/// A task that represents the asynchronously running view action. This task can be used to wait for 
		/// completion, to register a continuation and to access the state of the view action execution.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public Task SendAsync(ViewAction viewAction)
		{
			return ViewHelper.SendAsync(this, viewAction);
		}

		#endregion

		#region ++ Public Interface ++

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		public ViewUserControl()
		{
			if (Designer.IsRuntimeMode)
			{
				mDispatcher = ViewHelper.CreateDispatcher(this);
				ViewHelper.SetupViewActionEventHandler(this, mDispatcher);
				ViewHelper.SetupLoadedEventHandler(this, OnLocalize, OnLoaded);
			}
		}

		#endregion

		#region ## Overridable Methods ##

		/// <summary>
		/// This overridable method is called once after the view has been loaded. This method is called directly
		/// before <see cref="OnLoaded"/>. Override this method to localize static text resources.
		/// </summary>
		protected virtual void OnLocalize()
		{
			// default implementation
		}

		/// <summary>
		/// This overridable method is called once after the view has been loaded. This method is called directly
		/// after <see cref="OnLocalize"/>. Override this method to implement specific logic requiring the view to 
		/// be fully initialized and loaded, e.g. the entire tree of UI elements is loaded and the view model is 
		/// assigned to the view at this moment.
		/// </summary>
		protected virtual void OnLoaded()
		{
			// default implementation
		}

		#endregion

	}
}
